<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app"></div>
  <template id="my-app">
    <h2>今天温度：{{temp}} 度</h2>
    <h3>建议穿：{{suggestion}}</h3>

    <button @click="add">+</button>
    <button @click="minus">-</button>
  </template>


  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <script>
    const App = {
      template: '#my-app',
      data() {
        return {
          temp: 30,
          suggestion: '夹克'
        }
      },
      watch: { // 侦听器
        temp: {
          handler: function (newVal, oldVal) {
            if (newVal >= 30) {
              this.suggestion = '短袖'
            } else if (newVal <= 0) {
              this.suggestion = '羽绒服'
            } else {
              this.suggestion = '夹克'
            }
          },
          immediate: true // 立即执行
        }

        // temp: function(newVal, oldVal) {
        //   // console.log(newVal, oldVal);
        //   if (newVal >= 30) {
        //     this.suggestion = '短袖'
        //   } else if (newVal <= 0) {
        //     this.suggestion = '羽绒服'
        //   } else {
        //     this.suggestion = '夹克'
        //   }
        // }
      },
      methods: {
        add() {
          this.temp += 5
        },
        minus() {
          this.temp -= 5
        }
      }
    }
    Vue.createApp(App).mount('#app')
  </script>
</body>

</html>